﻿using System;
using Songhay;
using Songhay.Extensions;
using System.Collections.Generic;
using Songhay.DataAccess.Models;
using Songhay.Models;

namespace Songhay.DataAccess.Models.Extensions
{

    /// <summary>
    /// Extensions of <see cref="IFragment"/>
    /// </summary>
    public static class IFragmentExtensions
    {
        /// <summary>
        /// Gets the content of the fragment.
        /// </summary>
        /// <param name="fragment">The fragment.</param>
        public static string GetFragmentContent(this IFragment fragment)
        {
            if (fragment == null) return null;
            return fragment.ItemChar ?? fragment.ItemText;
        }

        /// <summary>
        /// Sets the defaults.
        /// </summary>
        /// <param name="fragment">The fragment.</param>
        public static void SetDefaults(this IFragment fragment)
        {
            if (fragment == null) return;

            fragment.CreateDate = DateTime.Now;
            fragment.IsActive = true;
            fragment.ModificationDate = DateTime.Now;
        }

        /// <summary>
        /// Converts the <see cref="IFragment" /> into a <see cref="DisplayItemModel"/>.
        /// </summary>
        /// <param name="fragment">The IFragment.</param>
        public static DisplayItemModel ToDisplayItemModel(this IFragment fragment)
        {
            return fragment.ToDisplayItemModel(DisplayItemModelCategories.GenericWebFragment, false);
        }

        /// <summary>
        /// Converts the <see cref="IFragment" /> into a <see cref="DisplayItemModel"/>.
        /// </summary>
        /// <param name="fragment">The fragment.</param>
        /// <param name="category">The category.</param>
        public static DisplayItemModel ToDisplayItemModel(this IFragment fragment, string category)
        {
            return fragment.ToDisplayItemModel(category, false);
        }

        /// <summary>
        /// Converts the <see cref="IFragment" /> into a <see cref="DisplayItemModel"/>.
        /// </summary>
        /// <param name="fragment">The IFragment.</param>
        /// <param name="category">The category.</param>
        /// <param name="copyFragmentContent">if set to <c>true</c> copy fragment content.</param>
        public static DisplayItemModel ToDisplayItemModel(this IFragment fragment, string category, bool copyFragmentContent)
        {
            if (fragment == null) return null;

            var data = new DisplayItemModel();
            if (copyFragmentContent) data.Description = fragment.ItemChar;
            data.DisplayText = fragment.FragmentDisplayName;
            data.Id = fragment.FragmentId;
            data.ItemCategory = category;
            data.ItemName = fragment.FragmentName;

            return data;
        }

        /// <summary>
        /// Converts the <see cref="IFragment"/> into a display text.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <returns></returns>
        public static string ToDisplayText(this IFragment data)
        {
            if (data == null) return null;

            var s = string.Format("FragmentId: {0}, FragmentName: {1}, IsActive: {2}, DocumentId: {3}",
                data.FragmentId, data.FragmentName, data.IsActive, data.DocumentId);
            if (!string.IsNullOrEmpty(data.FragmentDisplayName)) s += string.Format(", FragmentDisplayName:{0}", data.FragmentDisplayName);
            if (!string.IsNullOrEmpty(data.ItemChar)) s += string.Format(", ItemChar: {0}", data.ItemChar.Truncate(32));
            if (!string.IsNullOrEmpty(data.ItemText)) s += string.Format(", ItemText: {0}", data.ItemText.Truncate(32));
            s += string.Format(", PrevFragmentId: {0}, NextFragmentId: {1}", data.PrevFragmentId, data.NextFragmentId);
            s += string.Format(", IsNext: {0}, IsPrevious: {1}, IsWrapper: {2}", data.IsNext, data.IsPrevious, data.IsWrapper);
            return s;
        }

        /// <summary>
        /// Converts the <see cref="IFragment"/> into a menu display item model.
        /// </summary>
        /// <param name="fragment">The fragment.</param>
        public static MenuDisplayItemModel ToMenuDisplayItemModel(this IFragment fragment)
        {
            return fragment.ToMenuDisplayItemModel(DisplayItemModelCategories.GenericWebFragment, false);
        }

        /// <summary>
        /// Converts the <see cref="IFragment"/> into a menu display item model.
        /// </summary>
        /// <param name="fragment">The fragment.</param>
        /// <param name="category">The category.</param>
        public static MenuDisplayItemModel ToMenuDisplayItemModel(this IFragment fragment, string category)
        {
            return fragment.ToMenuDisplayItemModel(category, false);
        }

        /// <summary>
        /// Converts the <see cref="IFragment"/> into a menu display item model.
        /// </summary>
        /// <param name="document">The document.</param>
        /// <param name="category">The category.</param>
        /// <param name="copyFragmentContent">if set to <c>true</c> [copy fragment content].</param>
        public static MenuDisplayItemModel ToMenuDisplayItemModel(this IFragment document, string category, bool copyFragmentContent)
        {
            if (document == null) return null;

            var item = document.ToDisplayItemModel(category, copyFragmentContent);
            var menuItem = new MenuDisplayItemModel();
            FrameworkTypeUtility.SetProperties(item, menuItem);
            return menuItem;
        }

        /// <summary>
        /// Updates the specified fragment.
        /// </summary>
        /// <param name="fragment">The fragment.</param>
        /// <param name="data">The data.</param>
        public static void Update(this IFragment fragment, IFragment data)
        {
            if (fragment == null) return;
            if (data == null) return;

            FrameworkTypeUtility.SetProperties(data, fragment);
        }
    }
}
